package qrcode;

public class VersionHelper {

  public static Version getNearestVersion(BitHelper versionBits){
    int minDistance = Integer.MAX_VALUE;
    Version bestResult = null;
    for (Version version: Version.values()){
      if (!version.hasVersionInformation())
        continue;
      int distance = new BitHelper(version.getVersionInformationBits(), 18).getHammingDistance(versionBits);
      if (distance < minDistance){
        bestResult = version;
        minDistance = distance;
      }
    }
    if (minDistance > 3)
      return null; // see spec Appendix D.2
    return bestResult;
  }
}
